const app = getApp()
const db = wx.cloud.database();

let pageNav = [
  '../historyList/historyList',
  '../removeBind/removeBind',
  ''
]

Component({
  options: {
    addGlobalClass: true,
  },
  data: {
    user : app.globalData.allInfo,
    isStudent:null,
    list :[
      {
        icon:'taoxiaopu' ,
        color:'mauve',
        text:"签到记录"
      },
      {
        icon:'fork' ,
        color:'orange',
        text:"解除绑定"
      },
      {
        icon:'markfill' ,
        color: 'olive',
        text:"我的评论"
      }
    ]
  },





  // 在组件实例进入页面节点树时执行

  onGetOpenid: function () {

  },

  // 查询用户信息
  isBind() {
    db.collection("users").where({
      openid: app.globalData.openid
    }).get().then(res => {
  
      app.globalData.allInfo = res.data[0]
      console.log(res)
      console.log(app.globalData)

    })
  },

methods:{

  // 导航栏跳转
  goPage(e) {
    console.log(e.currentTarget.dataset.index)
    let index = e.currentTarget.dataset.index
    let toPage = pageNav[index]
    wx.navigateTo({
      url: toPage, //跳转到指定页面
    })
    console.log(typeof(toPage))

  },
  
},

  attached() {
    // 判断是否是学生
    if (app.globalData.isStudent == '1') {
      this.setData({
        isStudent:true
      })
    }
    else{
      this.setData({
        isStudent:false
      })
    }

      // 调用云函数
      if(!app.globalData.allInfo){
        wx.cloud.callFunction({
          name: 'login',
          data: {},
          success: res => {
            app.globalData.openid = res.result.openid
            console.log(app.globalData)
            console.log('[云函数] [login] user openid: ', app.globalData.openid)
            if(app.globalData.isStudent == '1'){
              db.collection("users").where({
                openid: app.globalData.openid
              }).get().then(res => {
            
                app.globalData.allInfo = res.data[0]
                console.log(res)
                console.log(app.globalData)
                this.setData({
                  user:app.globalData.allInfo,
                  isStudent:true
                })
          
              })


            }
            else{
              db.collection("teacher_users").where({
                openid: app.globalData.openid
              }).get().then(res => {
            
                app.globalData.allInfo = res.data[0]
                console.log(res)
                console.log(app.globalData)
                this.setData({
                  user:app.globalData.allInfo,
                  isStudent:false

                })
          
              })
            }


    
          },
          fail: err => {
            console.error('[云函数] [login] 调用失败', err)
          }
        })


      }
      else{
        this.setData({
          user:app.globalData.allInfo
        })
      }


     


  },




})